//
//  NSDate+LSCategory.m
//  比较时间字符串大小
//
//  Created by chenleping on 2018/12/12.
//  Copyright © 2018年 DSY. All rights reserved.
//

#import "NSDate+LSCategory.h"

@implementation NSDate (LSCategory)

// NSDate转NSString
- (NSString *)ls_convertToStringWithFormat:(NSString *)format{
    NSDateFormatter * formatter = [NSDateFormatter new];
    [formatter setDateFormat:format];
    NSString * dateStr = [formatter stringFromDate:self];
    return dateStr;
}
// 判断是否是今天
- (BOOL)ls_isToday{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}
// 判断是否是昨天
- (BOOL)ls_isYesterday{
    NSDate *nowDate = [[NSDate date] ls_dateWithYMD];
    NSDate *selfDate = [self ls_dateWithYMD];
    // 获得nowDate和selfDate的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day == 1;
}
- (NSDate *)ls_dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}


@end
